package com.mldkhn.led_controller;

import java.net.Socket;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

@SuppressLint("DefaultLocale")
public class Fragment_Farbrad extends Fragment implements OnSeekBarChangeListener, OnClickListener {
		
		private static TextView lblRadDelay, lblRadB;
		private static SeekBar scrRadDelay, scrRadB;
		private static Button btnRadStart;
		private static Button btnRadStop;
		private static String strWheel = "WHEEL";
		private static String strStop = "OFF";
		private int progressB = 0, progressDelay = 0;
		
		private Socket socket;

		private static int SERVERPORT;
		private static String SERVER_IP;
		Thread socketThread;
		
		public Fragment_Farbrad() {
		}
		
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
		    Bundle savedInstanceState) {
		    if (container == null) {
		        return null;
		    }
		    View view = inflater.inflate(R.layout.fragment_farbrad, container, false);
		    btnRadStart = (Button)view.findViewById(R.id.btnRadStart);
		    btnRadStart.setOnClickListener(this);
		    btnRadStop = (Button)view.findViewById(R.id.btnRadStop);
		    btnRadStop.setOnClickListener(this);
		    lblRadDelay = (TextView)view.findViewById(R.id.lblRadDelay);
		    lblRadB = (TextView)view.findViewById(R.id.lblRadB);
			scrRadDelay = (SeekBar)view.findViewById(R.id.scrVielfarbeDelay);
			scrRadB = (SeekBar)view.findViewById(R.id.scrRadB);
			lblRadDelay.setText(" 0.0 s");
			lblRadB.setText(" 0 %");
			scrRadDelay.setOnSeekBarChangeListener(this);
			scrRadB.setOnSeekBarChangeListener(this);
			try{
				SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(view.getContext());
				SERVER_IP = mySharedPreferences.getString("edittext_ip", "");
				SERVERPORT = Integer.parseInt(mySharedPreferences.getString("edittext_port", ""));
				socketThread = new Thread(new ClientThread());
				socketThread.start();
				PrintWriter out = new PrintWriter(new BufferedWriter(
						new OutputStreamWriter(socket.getOutputStream())),
						true);
				out.println("HELLO");
				out.close();
				socketThread.interrupt();
				socketThread = null;
				
				} 
				  catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
		    return view;
			}

		   @SuppressLint("DefaultLocale")
		@Override
		    public void onClick(View v) {
		        switch (v.getId()) {
		        case R.id.btnRadStart:

					try{
						SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(v.getContext());
						SERVER_IP = mySharedPreferences.getString("edittext_ip", "");
						SERVERPORT = Integer.parseInt(mySharedPreferences.getString("edittext_port", ""));
						socketThread = new Thread(new ClientThread());
						socketThread.start();
						PrintWriter out = new PrintWriter(new BufferedWriter(
								new OutputStreamWriter(socket.getOutputStream())),
								true);
						String B, Delay;
						B = String.format("%03d", progressB);
						Delay = String.format("%02d", progressDelay);
						out.println(strWheel + "\n" + B + "\n" + Delay);
						out.close();
						socketThread.interrupt();
						socketThread = null;
						
						} 
						  catch (UnknownHostException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						} catch (Exception e) {
							e.printStackTrace();
						}

		            break;
		        case R.id.btnRadStop:
					try{
						SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(v.getContext());
						SERVER_IP = mySharedPreferences.getString("edittext_ip", "");
						SERVERPORT = Integer.parseInt(mySharedPreferences.getString("edittext_port", ""));
						socketThread = new Thread(new ClientThread());
						socketThread.start();
						PrintWriter out = new PrintWriter(new BufferedWriter(
								new OutputStreamWriter(socket.getOutputStream())),
								true);
						out.println(strStop);
						socketThread.interrupt();
						socketThread = null;
						
						} 
						  catch (UnknownHostException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						} catch (Exception e) {
							e.printStackTrace();
						}
		        	
		        }
		    }
		
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			switch(seekBar.getId()){
				case R.id.scrVielfarbeDelay:
					lblRadDelay.setText(" " + (float)progress/2 + " s");
					progressDelay = progress;
					break;
				case R.id.scrRadB:
					lblRadB.setText(" " + progress + " %");
					progressB = progress;
					break;
			}
			
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}
		
		class ClientThread implements Runnable {
			
			@Override
			public void run() {
				
				try {
					InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

					socket = new Socket(serverAddr, SERVERPORT);

				} catch (UnknownHostException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}


		}

	}

